package com.lx.ngsl.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//WebMvcConfigurerAdapterSpring内部的一种配置方式
//采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
@SuppressWarnings("deprecation")
@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {
	@Autowired
	private Interceptor interceptor;
	@Autowired
	private AppInterceptor appInterceptor;
	@Value("${app.file-path}")
	protected String uploadLocation;
	
	/**
	 * 添加拦截器,排除不拦截的路径(拦截的是控制器，因此路径是控制器上配置的url)
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//登录拦截的管理器
		InterceptorRegistration registration = registry.addInterceptor(interceptor);
		registration.excludePathPatterns("/v0/login");
		registration.excludePathPatterns("/register/company","/imgCode/**");
		registration.excludePathPatterns("/error");
		 //静态资源不需要拦截 
        registration.excludePathPatterns("/**.html");
        registration.excludePathPatterns("/scripts/**");
        registration.excludePathPatterns("/assets/**");
        registration.excludePathPatterns("/css/**");
        registration.excludePathPatterns("/img/**");
        registration.excludePathPatterns("/lib/**");
        registration.excludePathPatterns("/modules/**");
        registration.excludePathPatterns("/exam/**");
        registration.excludePathPatterns("**/swagger-resources/**");
        registration.excludePathPatterns("**/swagger-ui.html/**");
        registration.excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/*.html", "/**/*.html","/swagger-resources/**");
        
        registration.excludePathPatterns("/api/exam/**");
        registration.excludePathPatterns("/api/v0/**");
        registration.excludePathPatterns("/autopaper/paperquery");
        registration.excludePathPatterns("/history/historyquery");
        registration.excludePathPatterns("/v0/user/**");
        
        
//        registry.addInterceptor(appInterceptor)
//        .excludePathPatterns("/**")
//        .addPathPatterns("/api/exam/**");
//        registration.excludePathPatterns("/autopaper/paperquery/**");
        
	}
	
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry
			.addResourceHandler(uploadLocation+"/**")
			.addResourceLocations("file:"+uploadLocation);
	}
	/**
	 * 
	 * <p>Method ：addCorsMappings
	 * <p>Description : 设置跨域访问
	 *
	 * @param registry 
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry)
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/api/v0/**")
			.allowedMethods("*")
			.allowedHeaders("*")
			.allowedOrigins("*");
		registry.addMapping("/v0/user/**")
		.allowedMethods("put")
		.allowedHeaders("*")
		.allowedOrigins("*");
		registry.addMapping("/autopaper/paperquery")
		.allowedMethods("put")
		.allowedHeaders("*")
		.allowedOrigins("*");
	}
}
